package fun.tryto.ctp;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.lang.Console;
import cn.hutool.json.JSONUtil;

public class TraderSpiImpl extends CThostFtdcTraderSpi {
	final static String brokerID = "9999";
	final static String userID = "13910072705";
	final static String password = "Abcd1234%^&*";
	final static String investorID = "104296";
	final static String appID = "simnow_client_test";
	final static String appCode = "0000000000000000";
	private List<String> ListExchanges = new ArrayList<String>();
	private List<String> ListProducts = new ArrayList<String>();
	private List<String> ListInstruments = new ArrayList<String>();

	private CThostFtdcTraderApi traderApi;

	TraderSpiImpl(CThostFtdcTraderApi traderApi) {
		this.traderApi = traderApi;
	}

	@Override
	public void OnFrontConnected() {
		Console.log("OnFrontConnected");
		CThostFtdcReqAuthenticateField clientAuth = new CThostFtdcReqAuthenticateField();
		clientAuth.setBrokerID(brokerID);
		clientAuth.setUserID(userID);
		clientAuth.setAppID(appID);
		clientAuth.setAuthCode(appCode);
		clientAuth.setUserProductInfo(appID);
		int b = traderApi.ReqAuthenticate(clientAuth, 0);
		Console.log("b:" + b);
		Console.log("Send auth ok");
	}

	@Override
	public void OnRspAuthenticate(CThostFtdcRspAuthenticateField field, CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
		Console.log("OnRspAuthenticate:{}:{}:{}", bIsLast, JSONUtil.toJsonStr(field), JSONUtil.toJsonStr(pRspInfo));
		if (pRspInfo != null && pRspInfo.getErrorID() != 0) {
			System.out.printf("Auth ErrorID[%d] ErrMsg[%s]\n", pRspInfo.getErrorID(), pRspInfo.getErrorMsg());
			return;
		}
		Console.log("Auth success!!!");
		CThostFtdcReqUserLoginField userLogin = new CThostFtdcReqUserLoginField();
		userLogin.setBrokerID(brokerID);
		userLogin.setUserID(investorID);
		userLogin.setPassword(password);
		userLogin.setUserProductInfo("JAVA_API");
		int a = traderApi.ReqUserLogin(userLogin, 1);
		Console.log("a:" + a);
		Console.log("Send login ok");
	}

	@Override
	public void OnRspUserLogin(CThostFtdcRspUserLoginField pRspUserLogin, CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
		Console.log("OnRspUserLogin:{}:{}:{}", bIsLast, JSONUtil.toJsonStr(pRspUserLogin), JSONUtil.toJsonStr(pRspInfo));
		if (pRspInfo != null && pRspInfo.getErrorID() != 0) {
			System.out.printf("Login ErrorID[%d] ErrMsg[%s]\n", pRspInfo.getErrorID(), pRspInfo.getErrorMsg());
			return;
		}
		Console.log(pRspUserLogin);
		Console.log("Login success!!!");
		CThostFtdcQryExchangeField pQryExchange = new CThostFtdcQryExchangeField();
		int b = traderApi.ReqQryExchange(pQryExchange, 1);
		Console.log("b:" + b);
	}

	@Override
	public void OnRspQryExchange(CThostFtdcExchangeField exchange, CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
		Console.log("OnRspQryExchange:{}:交易所代码={},交易所名称={}", bIsLast, exchange.getExchangeID(), exchange.getExchangeName());
		ListExchanges.add(exchange.toString());
		if (bIsLast) {
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			CThostFtdcQryProductField pQryProduct = new CThostFtdcQryProductField();
			int c = traderApi.ReqQryProduct(pQryProduct, 2);
		}
	}

	@Override
	public void OnRspQryProduct(CThostFtdcProductField product, CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
		Console.log("OnRspQryProduct:{}:产品名称={},产品代码={},交易所代码={}", bIsLast, product.getProductName(), product.getProductID(), product.getExchangeID());
		ListProducts.add(product.toString());
		if (bIsLast) {
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			CThostFtdcQryInstrumentField pQryInstrument = new CThostFtdcQryInstrumentField();
			int d = traderApi.ReqQryInstrument(pQryInstrument, nRequestID);
			Console.log("d:" + d);
		}
	}

	@Override
	public void OnRspQryInstrument(CThostFtdcInstrumentField instrument, CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
		Console.log("OnRspQryInstrument:{}:合约名称={},合约代码={},产品代码={}", bIsLast, instrument.getInstrumentName(), instrument.getInstrumentID(), instrument.getProductID());
		ListInstruments.add(instrument.toString());
	}
}